package com.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.FamiliesInfo;
import com.Service.FamiliesService;

@Service("FamiliesService")
public class FamiliesServiceImpl extends BaseServiceImpl<FamiliesInfo>
		implements FamiliesService {

	List<FamiliesInfo> familylist = new ArrayList<FamiliesInfo>();

	@Autowired
	private BaseDaoI<FamiliesInfo> familydao;

	@Override
	public int checkByfamilyName(String familyName) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyName", familyName);

		String hql = "from FamiliesInfo t where t.familyName = :familyName ";
		familylist = this.familydao.find(hql, params);
		if (!familylist.isEmpty() && familylist.get(0).getFamilyId() > 0) {
			return familylist.get(0).getFamilyId();
		} else {
			return 0;
		}
	}

	@Override
	public List<FamiliesInfo> getListByAdminId(int userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);

		String hql = "from FamiliesInfo t where t.adminId = :userid ";

		familylist = this.familydao.find(hql, params);
		if (null != familylist && !familylist.isEmpty()) {
			return familylist;
		} else {
			return null;
		}
	}

	@Override
	public List<FamiliesInfo> getListByFamilyId(int familyid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyid", familyid);

		String hql = "from FamiliesInfo t where t.familyId = :familyid ";
		familylist = this.familydao.find(hql, params);
		if (null != familylist && !familylist.isEmpty()) {
			return familylist;
		} else {
			return null;
		}
	}

	@Override
	public int deleteFamily(int familyid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyid", familyid);
		String hql = "from FamiliesInfo t where t.familyId = :familyid ";
		familylist = this.familydao.find(hql, params);
		if (null != familylist && !familylist.isEmpty()) {
			this.familydao.delete(familylist.get(0));
			return 1;
		} else {
			return 0;
		}

	}

}
